package com.tao.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tao.gulimall.product.dao.BrandDao;
import com.tao.gulimall.product.dao.CategoryDao;
import com.tao.gulimall.product.entity.BrandEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.product.dao.CategoryBrandRelationDao;
import com.tao.gulimall.product.entity.CategoryBrandRelationEntity;
import com.tao.gulimall.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
    @Resource
    private BrandDao brandDao;
    @Resource
    private CategoryDao categoryDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存
     * @param categoryBrandRelation
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        // 根据id查询名称
        String brandName = brandDao.selectById(brandId).getName();
        String catelogName = categoryDao.selectById(catelogId).getName();

        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(catelogName);
        this.baseMapper.insert(categoryBrandRelation);
    }

    /**
     * 更新当前的品牌相关信息
     * @param brandId
     * @param name
     */
    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
        relation.setBrandName(name);
        LambdaQueryWrapper<CategoryBrandRelationEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CategoryBrandRelationEntity::getBrandId, brandId);
        this.baseMapper.update(relation, lqw);
    }


    /**
     * 更新当前的级联关系中的分类信息
     * @param catId
     * @param name
     */
    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
        relation.setCatelogName(name);
        LambdaQueryWrapper<CategoryBrandRelationEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(CategoryBrandRelationEntity::getCatelogId,catId);
        this.baseMapper.update(relation, lqw);
    }


    /**
     * 根据分类id 查找当前所有的品牌的实体
     * @param catId
     * @return
     */
    @Override
    public List<BrandEntity> selectBrandByCatId(Long catId) {
        LambdaQueryWrapper<CategoryBrandRelationEntity> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(CategoryBrandRelationEntity::getCatelogId, catId);
        List<CategoryBrandRelationEntity> brandRelationEntities = this.baseMapper.selectList(lqw1);
        List<Long> brandIds = brandRelationEntities.stream()
                .map(CategoryBrandRelationEntity::getBrandId)
                .collect(Collectors.toList());
        // 再根据所有的品牌id 找到所有的品牌实体
        return brandDao.selectBatchIds(brandIds);
    }

}